package main

import "fmt"

/*函数返回两个数的最大值*/
func max(num1, num2 int) int {
	/*声明局部变量*/
	var result int
	if (num1 > num2) {
		result = num1
	} else {
		result = num2
	}
	return  result
}

/*函数可以返回多个值*/
func swap(x, y string) (string, string) {
	return y, x
}

/*通过引用交换*/
func swap2(x *int, y *int) {
	var temp int
	temp = *x
	*x = *y
	*y = temp
}

/*函数闭包*/
func getSequence() func() int {
	i := 0
	return func() int {
		i += 1
		return i
	}
}

/*定义结构体*/
type Circle struct {
	radius float64
}

/*此方法属于Circle类型对象中的方法*/
func (c Circle) getArea() float64 {
	return 3.14 * c.radius * c.radius
}

func main() {
	/*
	//定义局部变量
	var a int = 100
	var b int = 200
	var ret int

	//调用函数并返回最大值
	ret = max(a, b)
	fmt.Printf("最大值是：%d\n", ret)*/

	/*a, b := swap("Google", "Runoob")
	fmt.Println(a, b)*/

	/*var a int = 100
	var b int = 200
	fmt.Printf("交换前a的值：%d\n", a)
	fmt.Printf("交换前b的值：%d\n", b)
	swap2(&a, &b)
	fmt.Printf("交换后a的值：%d\n", a)
	fmt.Printf("交换后b的值：%d\n", b)*/

	//声明函数变量
	/*getSquareRoot := func(x float64) float64 {
		return math.Sqrt(x)
	}
	fmt.Println(getSquareRoot(9))*/

	/*// nextNumber为一个函数，函数i为0
	nextNumber := getSequence()
	// 调用nextNumber函数，i变量自增1并返回
	fmt.Println(nextNumber())
	fmt.Println(nextNumber())
	fmt.Println(nextNumber())
	// 创建新的函数nextNumber1
	nextNumber1 := getSequence()
	fmt.Println(nextNumber1())
	fmt.Println(nextNumber1())
	fmt.Println(nextNumber1())*/

	var c1 Circle
	c1.radius = 10.00
	fmt.Println("圆的面积 = ", c1.getArea())
}
